package net.lulihu.pangolin.headless.common.util;

import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import net.lulihu.pangolin.headless.common.protocol.ProtocolMessage;

/**
 * 通道数据转移
 */
public class ChannelDataTransferKit {

    /**
     * 数据转移
     */
    public static void transferData(ChannelHandlerContext ctx, ProtocolMessage msg) {
        Channel channel = ctx.channel();

        Channel bindChannel = ChannelTwoWayBindKit.getBindChannel(channel);
        if (bindChannel != null) {
            byte[] data = msg.getBody();
            ByteBuf buf = ctx.alloc().buffer(data.length);
            buf.writeBytes(data);
            bindChannel.writeAndFlush(buf);
        } else {
            channel.close();
        }
    }


}
